package com.tarena.lbs.activity.controller;

import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.param.ActivityUpdateStatusParam;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * 活动管理控制器
 * 提供活动的CRUD操作接口，供前端调用
 */
@Slf4j
@RestController
@Tag(name = "活动服务", description = "活动服务相关接口")
@RequestMapping("/admin/marketing/marketingActivity/info")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 商家活动列表分页查询
     * @param query 查询参数，包含分页信息和查询条件
     * @return 活动列表分页结果
     * @throws BusinessException 业务异常
     */
    @Operation(summary = "商家活动列表", description = "获取商家的活动列表，支持分页和条件查询")
    @PostMapping("/list")
    public Result<PageResult<ActivityVO>> pageList(@RequestBody ActivityQuery query)
        throws BusinessException {
        log.info("[Controller] 查询活动列表，参数：{}", query);
        try {
            PageResult<ActivityVO> pageResult = activityService.pageList(query);
            log.info("[Controller] 查询活动列表成功，总条数：{}", pageResult.getTotal());
            return new Result<>(pageResult);
        } catch (BusinessException e) {
            log.error("[Controller] 查询活动列表业务异常：{}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("[Controller] 查询活动列表系统异常", e);
            throw new BusinessException("500","查询活动列表失败");
        }
    }

    /**
     * 保存商家活动信息
     * @param param 活动参数，包含活动的各项信息
     * @return 操作结果
     * @throws BusinessException 业务异常
     * @throws ParseException 日期解析异常
     */
    @Operation(summary = "商家活动保存", description = "添加或更新商家活动信息")
    @PostMapping("/save")
    public Result<Void> activitySave(@RequestBody @Validated ActivityAddParam param)
            throws BusinessException {
        log.info("[Controller] 保存活动信息，参数：{}", param);
        try {
            activityService.saveActivity(param);
            log.info("[Controller] 保存活动信息成功");
            return Result.success();
        } catch (ParseException e) {
            log.error("[Controller] 日期格式错误：{}", e.getMessage());
            throw new BusinessException("400","日期格式错误，请检查开始时间和结束时间");
        } catch (BusinessException e) {
            log.error("[Controller] 保存活动信息业务异常：{}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("[Controller] 保存活动信息系统异常", e);
            throw new BusinessException("500","保存活动信息失败");
        }
    }

    /**
     * 获取商家活动详情
     * @param id 活动ID
     * @return 活动详情信息
     * @throws BusinessException 业务异常
     */
    @Operation(summary = "商家活动详情", description = "获取指定活动的详细信息")
    @GetMapping("/detail/{id}")
    public Result<ActivityVO> activityDetail(
            @Parameter(description = "活动ID") @PathVariable("id") Long id)
        throws BusinessException {
        log.info("[Controller] 查询活动详情，ID：{}", id);
        try {
            ActivityVO activityVO = activityService.getActivityById(id);
            log.info("[Controller] 查询活动详情成功");
            return new Result<>(activityVO);
        } catch (BusinessException e) {
            log.error("[Controller] 查询活动详情业务异常：{}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("[Controller] 查询活动详情系统异常", e);
            throw new BusinessException("500","查询活动详情失败");
        }
    }

    //POST /admin/marketing/marketingActivity/info/updateStatus
    @Operation(summary = "修改活动状态")
    @PostMapping("/updateStatus")
    public Result<Void> updateStatus(@RequestBody ActivityUpdateStatusParam param) throws BusinessException {
        log.debug("修改活动状态入参:{}",param);
        try {
            // 验证参数有效性
            if (param.getStatus() != 0 && param.getStatus() != 1) {
                throw new BusinessException("400","启用状态值无效，只能是0或1");
            }
            // 直接调用service层的方法
            activityService.enableActivity(param.getId(), param.getStatus());
            log.info("[Controller] 启用/禁用活动成功");
            return Result.success();
        } catch (Exception e) {
            log.error("[Controller] 启用/禁用活动业务异常：{}", e.getMessage());
            throw new BusinessException("500","启用/禁用活动失败");
        }
    }

    //POST /admin/marketing/marketingActivity/info/update
    @Operation(summary = "修改活动信息")
    @PostMapping("update")
    public Result<Void> update(@RequestBody ActivityAddParam param) throws BusinessException, ParseException {
        log.debug("修改活动信息入参:{}",param);
        activityService.saveActivity(param);
        return Result.success();
    }
}
